//package com.herench.bridge.impl;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.herench.bridge.ReconciliationService;
//import com.herench.bridge.StrangeMessage;
//import com.herench.commons.utils.DesUtil;
//import com.herench.commons.utils.HttpClientSupport;
//import org.apache.log4j.Logger;
//import org.dom4j.DocumentHelper;
//import org.dom4j.Element;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//import org.springframework.util.Assert;
//import org.springframework.util.StringUtils;
//
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * Created by ThinkPad on 2018/6/4.
// */
//@Component("reconciliationService")
//@javax.jws.WebService(endpointInterface = "com.herench.bridge.ReconciliationService", serviceName = "reconciliationService", targetNamespace = "http://bridge.herench.com/")
//public class ReconciliationServiceImpl implements ReconciliationService {
//
//
//    private static final Logger logger = Logger.getLogger(ReconciliationServiceImpl.class);
//
//    @Value("${payUrl}")
//    private String payUrl;
//
//    @Value("${wepayChannelCode}")
//    private String wepayChannelCode;
//
//    @Value("${alipayChannelCode}")
//    private String alipayChannelCode;
//
//    @Override
//    public String recAlipay(String msg) {
//        return innerRec(msg, alipayChannelCode);
//    }
//
//    @Override
//    public String recWepay(String msg) {
//        return innerRec(msg, wepayChannelCode);
//    }
//
//    private String innerRec(String msg, String channelCode) {
//        logger.info("request:" + msg);
//        StrangeMessage responseMessage = new StrangeMessage();
//        responseMessage.addAttrToClientInfo("METHOD", "qrPay");
//        try {
//            StrangeMessage requestMessage = new StrangeMessage(msg);
//
//            String dateStr = requestMessage.getAttrFromCommitDataRow("RECONCILIATION_DATE");
//            Assert.hasLength(dateStr, "RECONCILIATION_DATE 账目日期参数不能为空");
//            String method = requestMessage.getAttrFromClientInfo("METHOD");
//            Assert.hasLength(method, "METHOD 为空");
//            String moneyType = requestMessage.getAttrFromCommitDataRow("MONEY_TYPE");
//            Assert.hasLength(moneyType, "MONEY_TYPE 为空");
//
//            Map<String, Object> params = new HashMap<>();
//            params.put("billDate", dateStr);
//            params.put("channelCode", channelCode);
//            params.put("moneyType", moneyType);
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("funcode", "790001");
//            jsonObject.put("args", DesUtil.encrypt(JSON.toJSONString(params)));
//            String str = HttpClientSupport.doPost(payUrl, jsonObject.toString(), "UTF-8");
//            JSONObject resJson = JSON.parseObject(str);
//            if (resJson.getString("code").equals("0")) {
//                JSONObject data = resJson.getJSONObject("data");
//                String state = data.getString("state");
//                String stateDesc = data.getString("stateDesc");
//                JSONObject bills = data.getJSONObject("bills");
//                if (state.equals("0")) {
//                    if (method.equals("total")) {
//                        Element element = DocumentHelper.createElement("DataRow");
//                        element.addAttribute("TRANSACTION_COUNT", (Integer.valueOf(bills.getString("transCount")) + Integer.valueOf(bills.getString("refundCount"))) + "");
//                        element.addAttribute("TRANSACTION_AMOUNT", bills.getString("actualAmount"));
//                        responseMessage.addReturnDataRow(element);
//                    } else if (method.equals("detail")) {
//                        JSONArray array = bills.getJSONArray("billDetails");
//                        if (array != null && array.size() > 0) {
//                            for (int i = 0; i < array.size(); i++) {
//                                JSONObject temp = array.getJSONObject(i);
//                                Element element = DocumentHelper.createElement("DataRow");
//
//                                String terminalId = temp.getString("terminalId");
//                                if (StringUtils.hasLength(terminalId) && terminalId.equals(moneyType)) {
//                                    element.addAttribute("TERMINAL_NUMBER", temp.getString("channelCode"));
//                                    element.addAttribute("POS_NO", temp.getString("terminalId"));
//                                    element.addAttribute("TRAN_AMT", temp.getString("receiptAmount"));
//                                    element.addAttribute("TRAN_TYPE", temp.getString("tradeType"));
//                                    element.addAttribute("TRAN_DATE", temp.getString("startTime"));
//                                    element.addAttribute("CARD_ID", temp.getString("buyerId"));
//                                    String outRequestNo = temp.getString("outRequestNo");
//                                    if (StringUtils.hasLength(outRequestNo) && !outRequestNo.equals("0")) {
//                                        element.addAttribute("TRANS_NO", outRequestNo);
//                                    } else {
//                                        element.addAttribute("TRANS_NO", temp.getString("outTransNo"));
//                                    }
//                                    responseMessage.addReturnDataRow(element);
//                                }
//                            }
//                        }
//                    }
//                }
//                responseMessage.addExecuteStatus(state);
//                responseMessage.addExecuteMessage(stateDesc);
//            } else {
//                responseMessage.addExecuteStatus(resJson.getString("code"));
//                responseMessage.addExecuteMessage(resJson.getString("message"));
//            }
//        } catch (Exception e) {
//            responseMessage.addExecuteStatus("1");
//            if (e.getMessage() == null) {
//                responseMessage.addExecuteMessage("请求超时，请重试！");
//            } else {
//                responseMessage.addExecuteMessage(e.getMessage());
//            }
//            logger.info("异常信息:" + e.getCause());
//            e.printStackTrace();
//        }
//        logger.info("response:" + responseMessage);
//        return responseMessage.getBody().asXML();
//    }
//}
